package com.hezuti.sns.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hezuti.common.email.EmailService;
import com.hezuti.common.utils.ConfigUtil;
import com.hezuti.common.utils.StringUtil;
import com.hezuti.sns.dao.LserialDao;
import com.hezuti.sns.dao.UserDao;
import com.hezuti.sns.domain.Lserial;
import com.hezuti.sns.domain.User;

@Service
public class UserService {

	@Autowired
	private UserDao userDao; 
	@Autowired
	private LserialDao lserialDao;

	/**
	 * 添加用户
	 * @param User u
	 * @return
	 */
	public User doAddUser(User user) {
		Date d = new Date();
		user.setCtime(d);
		user.setUtype(User.USER_TYPE_NOMALL);//普通用户
		user.setUser_status(User.USER_STATUS_VALID);//有效状态
		user.setEmail_status((short)0);//邮箱没有验证
		user.setMobile_state((short)0);//手机没有验证
		user.setHasHeadPic(User.USER_HEAD_NO);
		return userDao.save(user);
	}

	/**
	 * 检查用户名与密码是否正确
	 * 
	 * @param loginName
	 * @param password
	 * @return
	 */
	public boolean doCheckPassword(String loginName, String password) {
		boolean result = false;
		User user = userDao.findUniqueBy(User.FIELD_LOGIN_NAME, loginName);
		if (user != null && user.getPassword().equals(password)) {
			result = true;
		}
		return result;
	}

	/**
	 * 检查用户名与密码是否正确
	 * 
	 * @param loginName
	 * @param password
	 * @return 返回用户对象
	 */
	public User doCheckPasswordByEmail(String email, String password) {
		User user = userDao.findUniqueBy(User.FIELD_EMAIL, email);
		if (user != null && user.getPassword().equals(password)) {
			return user;
		}
		return null;
	}
	public User doCheckPasswordByMobile(String mobile,String password){
		User user = userDao.findUniqueBy(User.FIELD_MOBILE, mobile);
		if (user != null && user.getPassword().equals(password)) {
			return user;
		}
		return null;
		
	}
	/**
	 *取出上次登录信息
	 * 记录本次登录
	 * @param uid
	 * @param ip
	 * @param loginType
	 * @return
	 */
	public Lserial doRecLoginOpreate(int uid,String ip,int loginType){
		Lserial l=lserialDao.getLastLoginInfo(uid);
		lserialDao.addRec(uid, ip, loginType);
		return l;
	}

	/**
	 * 修改用户密码
	 * 
	 * @param email
	 * @param oldPassword
	 * @param newPassword
	 */
	public void doChangePassword(String email, String oldPassword, String newPassword) {
		User user = userDao.findUniqueBy(User.FIELD_EMAIL, email);
		if (user != null && user.getPassword().equals(oldPassword)) {
			user.setPassword(newPassword);
			userDao.save(user);
		}
	}
	/**
	 * 绑定手机号
	 * 
	 * @param user
	 * @return 成功：1<BR/>
	 *         失败：已经绑定：2 不存在用户：-1
	 */
	public int doBindMobile(User user) {
		User u= userDao.load(user.getId());
		int r=2;
		if (u == null)
			r= -1;
		if (u.getMobile() == null||u.getMobile().trim().length()==0){
			u.setMobile(user.getMobile());
			r = 1;
		}
		return r;
	}

	/**
	 * 检测绑定手机状态
	 * 
	 * @param user
	 * @return 未绑定：0 已经绑定：2 不存在用户：-1
	 */
	public String doCheckAndGetUserMobile(User user) {
		user = userDao.load(user.getId());
		if (user == null)
			return "-1";
		if (user.getMobile() == null)
			return "0";
		return user.getMobile();
	}
	/**
	 * 用户权限<br/>
	 * 
	 * @return
	 */
	public boolean doHasRight(User user){
		//检查app状态
		
		//检查用户是否开通app
		
		return true;
	}

	/**
	 * 通过id 找 user
	 * @param uid
	 * @return
	 */
	public User getUserById(String uid) {
		return userDao.load(uid);
	}

	/**
	 * 上次登录记录
	 * @param id
	 * @return
	 */
	public Lserial getLastLoinInfo(int id) {
		return lserialDao.getLastLoginInfo(id);
	}
	/**
	 * 修改头像
	 * @param headpic
	 * @param id
	 */
	public void doModifyHeadPic(String headpic,int id){
		User user = userDao.findUniqueBy(User.FIELD_UID, id);
		if(user!=null){
			user.setHasHeadPic(User.USER_HEAD_HAS);
			user.setHeadPicName(headpic);
			userDao.save(user);
			}
	}
	/**
	 * 修改基本信息
	 * @param user
	 */
	public User doModifyUserInfo(String name,Short sex,Date birthday,String mobile,String email,int uid){
		User u=userDao.findUniqueBy(User.FIELD_UID,uid);
		if(u==null)return null;
		if(!StringUtil.isNull(name))u.setName(name);
		if(null!=sex){
			if(sex==User.USER_SEX_MAN)
				u.setGender(User.USER_SEX_MAN);
			if(sex==User.USER_SEX_WOMAN)
				u.setGender(User.USER_SEX_WOMAN);
		}
		if(null!=birthday)u.setBirthday(birthday);
		if(!StringUtil.isNull(mobile)&&!mobile.equals(u.getMobile())){
			u.setMobile(mobile);
			u.setMobile_state((short)0);//跟换新的手机后修改手机验证状态
		}
		if(!StringUtil.isNull(email)&&!u.getEmail().equals(email)){
			u.setEmail(email);
			u.setEmail_status((short)0);//修改 Email 后修改 email验证状态；
		}
		userDao.save(u);
		return u;
	}
	/**
	 * 发送email邮件
	 * @param userInfo
	 */
	public void doSendEmail(String to,String title,String content) {
		EmailService.getInstance().sendEmail(to, title, content);
	}
	public User doModifyEmailStatus(int uid){
		User u=userDao.findUniqueBy(User.FIELD_UID,uid);
		if(u!=null){
			u.setEmail_status(User.USER_EMAIL_PROV);
			userDao.save(u);
			return u;
		}
		return null;
	}

}
